import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User: YKH
 * Date: 2022-04-29
 * Time: 18:24
 * author:Test01
 */
public class Test01 {
    public static final String [] suit = {"♥","♠","♣","♦"};
    //创建一副牌
    public static List<Card> buyCard() {
        //创建一个顺序表放牌
        List<Card> cardlist= new ArrayList<>();
        for (int i = 1; i <= 13 ; i++) {
            for (int j = 0; j < 4; j++) {
                Card card = new Card( suit[j],i );
                //将每次创建的牌放入这副牌中
                cardlist.add(card);
            }
        }
        return cardlist;
    }
    //洗牌
    public static void shuffle(List<Card> cardlist) {
        //从后往前遍历顺序表,将这个元素和前面的元素随机进行交换
        for (int i = cardlist.size()-1; i >0 ; i--) {
            Random random = new Random();
            int index = random.nextInt(i);
            swap(cardlist,i,index);
        }
    }
    //交换
    private static void swap(List<Card> cardlist, int i, int index) {
        Card temp = cardlist.get(i);
        cardlist.set(i,cardlist.get(index));
        cardlist.set(index,temp);
    }
    //三个人轮流摸牌,每人摸5张
    public static  List<List<Card>> deal(List<Card> cardList ){
         //创建三个顺训表,分别存放3个人摸到手的牌
        List<Card> hand1 = new ArrayList<>();
        List<Card> hand2 = new ArrayList<>();
        List<Card> hand3 = new ArrayList<>();
        //创建一个二维数组,存放三个人手中的牌
        List<List<Card>> hands= new ArrayList<>();
        hands.add(hand1);
        hands.add(hand2);
        hands.add(hand3);
        //开始发牌
        for(int i= 0;i<5;i++) {
            for(int j=0;j<3;j++) {
                Card card = cardList.remove(0);
                hands.get(j).add(card);
            }
        }
        return hands;
    }

    public static void main(String[] args) {
        List<Card> cardList = buyCard();
        shuffle(cardList);
        System.out.println("洗牌: " + cardList);
        List<List<Card>> hands = deal(cardList);
        for(int i=0;i<3;i++) {
            System.out.println("第" + i+1 +"个人的牌:" + hands.get(i));
        }
        System.out.println("剩余的牌:" + cardList);
    }
}
